package com.gitee.cui.netty.bytebuf;

import com.gitee.cui.util.Logger;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import org.junit.Test;

import static com.gitee.cui.netty.bytebuf.PrintAttribute.print;

/**
 * ByteBuf的读写测试
 * [main|PrintAttribute.print] |>  after ===========动作：分配ByteBuf(9,100)============
 * [main|PrintAttribute.print] |>  1.0 isReadable(): false
 * [main|PrintAttribute.print] |>  1.1 readerIndex(): 0
 * [main|PrintAttribute.print] |>  1.2 readableBytes(): 0
 * [main|PrintAttribute.print] |>  2.0 isWritable(): true
 * [main|PrintAttribute.print] |>  2.1 writerIndex(): 0
 * [main|PrintAttribute.print] |>  2.2 writableBytes(): 9
 * [main|PrintAttribute.print] |>  3.0 capacity(): 9
 * [main|PrintAttribute.print] |>  3.1 maxCapacity(): 100
 * [main|PrintAttribute.print] |>  3.2 maxWritableBytes(): 100
 * [main|PrintAttribute.print] |>  after ===========动作：写入4个字节（1，2，3，4）============
 * [main|PrintAttribute.print] |>  1.0 isReadable(): true
 * [main|PrintAttribute.print] |>  1.1 readerIndex(): 0
 * [main|PrintAttribute.print] |>  1.2 readableBytes(): 4
 * [main|PrintAttribute.print] |>  2.0 isWritable(): true
 * [main|PrintAttribute.print] |>  2.1 writerIndex(): 4
 * [main|PrintAttribute.print] |>  2.2 writableBytes(): 5
 * [main|PrintAttribute.print] |>  3.0 capacity(): 9
 * [main|PrintAttribute.print] |>  3.1 maxCapacity(): 100
 * [main|PrintAttribute.print] |>  3.2 maxWritableBytes(): 96
 * [main|WriteReadTest.writeReadTest] |>  ===============start get:==================
 * [main|WriteReadTest.getByteBuf] |>  读取一个字节：1
 * [main|WriteReadTest.getByteBuf] |>  读取一个字节：2
 * [main|WriteReadTest.getByteBuf] |>  读取一个字节：3
 * [main|WriteReadTest.getByteBuf] |>  读取一个字节：4
 * [main|PrintAttribute.print] |>  after ===========动作：取数据ByteBuf============
 * [main|PrintAttribute.print] |>  1.0 isReadable(): true
 * [main|PrintAttribute.print] |>  1.1 readerIndex(): 0
 * [main|PrintAttribute.print] |>  1.2 readableBytes(): 4
 * [main|PrintAttribute.print] |>  2.0 isWritable(): true
 * [main|PrintAttribute.print] |>  2.1 writerIndex(): 4
 * [main|PrintAttribute.print] |>  2.2 writableBytes(): 5
 * [main|PrintAttribute.print] |>  3.0 capacity(): 9
 * [main|PrintAttribute.print] |>  3.1 maxCapacity(): 100
 * [main|PrintAttribute.print] |>  3.2 maxWritableBytes(): 96
 * [main|WriteReadTest.writeReadTest] |>  ===============start read:=================
 * [main|WriteReadTest.readByteBuf] |>  取一个字节：1
 * [main|WriteReadTest.readByteBuf] |>  取一个字节：2
 * [main|WriteReadTest.readByteBuf] |>  取一个字节：3
 * [main|WriteReadTest.readByteBuf] |>  取一个字节：4
 * [main|PrintAttribute.print] |>  after ===========动作：读完ByteBuf============
 * [main|PrintAttribute.print] |>  1.0 isReadable(): false
 * [main|PrintAttribute.print] |>  1.1 readerIndex(): 4
 * [main|PrintAttribute.print] |>  1.2 readableBytes(): 0
 * [main|PrintAttribute.print] |>  2.0 isWritable(): true
 * [main|PrintAttribute.print] |>  2.1 writerIndex(): 4
 * [main|PrintAttribute.print] |>  2.2 writableBytes(): 5
 * [main|PrintAttribute.print] |>  3.0 capacity(): 9
 * [main|PrintAttribute.print] |>  3.1 maxCapacity(): 100
 * [main|PrintAttribute.print] |>  3.2 maxWritableBytes(): 96
 * @author cuiqingqiang
 * @version 1.0
 * @date 2021/10/22 11:15
 **/
public class WriteReadTest {

    @Test
    public void writeReadTest() {
        ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(9, 100);
        print("动作：分配ByteBuf(9,100)", buffer);
        buffer.writeBytes(new byte[] {1, 2, 3, 4});
        print("动作：写入4个字节（1，2，3，4）", buffer);
        Logger.info("===============start get:==================");
        getByteBuf(buffer);
        print("动作：取数据ByteBuf", buffer);
        Logger.info("===============start read:=================");
        readByteBuf(buffer);
        print("动作：读完ByteBuf", buffer);
    }

    /**
     * 读取字节，会改变指针内容
     * @param buffer 缓冲区
     */
    private void readByteBuf(ByteBuf buffer) {
        while (buffer.isReadable()) {
            Logger.info("取一个字节：" + buffer.readByte());
        }
    }

    /**
     * 从通道中取字节，不改变指针属性
     * @param buffer 缓冲区
     */
    private void getByteBuf(ByteBuf buffer) {
        for (int i = 0; i < buffer.readableBytes(); i++) {
            Logger.info("读取一个字节：" + buffer.getByte(i));
        }
    }
}
